package com.bm.common.config;

import com.bm.common.RabbitMqConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMqConfig {
    @Bean
    public Queue mailQueue(){
        return new Queue(RabbitMqConstants.MQ_MAIL_QUEUE,
                RabbitMqConstants.durable,
                RabbitMqConstants.exclusive,
                RabbitMqConstants.autoDelete);
    }
    @Bean
    public Queue phoneQueue(){
        return new Queue(RabbitMqConstants.MQ_PHONE_QUEUE,
                RabbitMqConstants.durable,
                RabbitMqConstants.exclusive,
                RabbitMqConstants.autoDelete);
    }
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange(
                RabbitMqConstants.MQ_FANOUT_EXCHANGE,
                RabbitMqConstants.durable,
                RabbitMqConstants.autoDelete);
    }
    @Bean
    public Binding mailBinding(){
        return BindingBuilder.bind(mailQueue())
                .to(fanoutExchange());
    }
    @Bean
    public Binding phoneBinding(){
        return BindingBuilder.bind(phoneQueue())
                .to(fanoutExchange());
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange(
                RabbitMqConstants.MQ_TOPIC_EXCHANGE,
                RabbitMqConstants.durable,
                RabbitMqConstants.autoDelete);
    }
    @Bean
    public Binding mailBindingT(){
        return BindingBuilder.bind(mailQueue())
                .to(topicExchange())
                .with("topic.mail");
    }
    @Bean
    public Binding phoneBindingT(){
        return BindingBuilder.bind(phoneQueue())
                .to(topicExchange())
                .with("topic.phone");
    }


    //-------------------delay

    @Bean
    public Queue delayQueue(){
        return new Queue(RabbitMqConstants.MQ_DELAY_QUEUE,
                RabbitMqConstants.durable,
                RabbitMqConstants.exclusive,
                RabbitMqConstants.autoDelete);
    }
    @Bean
    public DirectExchange delayExchange(){
        return new DirectExchange(
                RabbitMqConstants.MQ_DELAY_EXCHANGE,
                RabbitMqConstants.durable,
                RabbitMqConstants.autoDelete);
    }
    @Bean
    public Binding delayBinding(){
        return BindingBuilder.bind(delayQueue())
                .to(delayExchange())
                .with(RabbitMqConstants.MQ_DELAY_ROUTING_KEY);
    }

    @Bean
    public Queue normalQueue(){
        Map<String,Object> map = new HashMap<>(2);
        map.put("x-dead-letter-exchange",RabbitMqConstants.MQ_DELAY_EXCHANGE);
        map.put("x-dead-letter-routing-key",RabbitMqConstants.MQ_DELAY_ROUTING_KEY);
        return new Queue(RabbitMqConstants.MQ_NORMAL_QUEUE,
                RabbitMqConstants.durable,
                RabbitMqConstants.exclusive,
                RabbitMqConstants.autoDelete,
                map);
    }
    @Bean
    public DirectExchange normalExchange(){
        return new DirectExchange(
                RabbitMqConstants.MQ_NORMAL_EXCHANGE,
                RabbitMqConstants.durable,
                RabbitMqConstants.autoDelete);
    }
    @Bean
    public Binding normalBinding(){
        return BindingBuilder.bind(normalQueue())
                .to(normalExchange())
                .with(RabbitMqConstants.MQ_NORMAL_ROUTING_KEY);
    }
}
